import moment from 'moment'
import common from '@/public/common.js'
/**
 * 数组倒序
 * @param {*} arr 倒序数组
 */
let filter = {
	/**
	 * 保留位数
	 * @param {*} num 数字
	 * @param {*} figure 位数
	 */
	toFixed: (num, figure) => {
		return num.toFixed(figure)
	},
	/**
	 * 数组倒叙
	 * @param {*} arr 数组
	 */
	reverseArray: (arr) => {
		var data = arr.reverse()
		return data
	},
	/**
	 * 座位类型
	 * @param {*} type 类型
	 */
	getSeatType: (type) => {
		let label = ''
		switch (type) {
			case 0:
				label = '普通座';
				break;
			case 1:
				label = '情侣座';
				break;
		}
		return label
	},
	/**
	 * 是否可以调座
	 * @param {*} type 类型
	 */
	canChangeSear: (type) => {
		let label = ''
		switch (type) {
			case 0:
				label = '不可调座';
				break;
			case 1:
				label = '可调座';
				break;
		}
		return label
	},
	/**
	 * 时间格式化
	 * @param {*} date 时间
	 * @param {*} format 格式
	 */
	formateDate: (date, format) => {
		return moment(date).utcOffset(8).format(format)
	},
	/**
	 * 时间格式化
	 * @param {*} date 时间
	 */
	getWeekByDay: (date) => {
		date = new Date(filter.formateDate(date, 'YYYY/MM/DD'))
		var d1 = date
		var d2 = new Date(filter.formateDate(new Date(), 'YYYY/MM/DD')) //new Date((new Date()).toLocaleDateString())
		var iday = parseInt(d1 - d2) / 1000 / 60 / 60 / 24
		if (iday === 0) {
			return '今天'
		}
		if (iday === 1) {
			return '明天'
		}
		if (iday === 2) {
			return '后天'
		}
		var week
		if (date.getDay() === 0) week = '周日'
		if (date.getDay() === 1) week = '周一'
		if (date.getDay() === 2) week = '周二'
		if (date.getDay() === 3) week = '周三'
		if (date.getDay() === 4) week = '周四'
		if (date.getDay() === 5) week = '周五'
		if (date.getDay() === 6) week = '周六'
		return week
	},
	/**
	 * 场次颜色
	 * @param {*} date 时间
	 */
	getWeekByDayStyle: (date) => {
		date = new Date(filter.formateDate(date, 'YYYY/MM/DD'))
		var d1 = date
		var d2 = new Date((new Date()).toLocaleDateString())
		var iday = parseInt(d1 - d2) / 1000 / 60 / 60 / 24
		if (iday === 0) {
			return '#ff003d'
		}
		if (iday === 1) {
			return '#185ae9'
		}
		if (iday === 2) {
			return '#FF9933'
		}
		return '#000'
	},
	/**
	 * 获取10位时间戳
	 * @param {*} date 时间
	 */
	getTimestamp: (date) => {
		return moment(date).utcOffset(8).valueOf()
	},
	/**
	 * 延长时间
	 * @param {*} date 时间
	 * @param {*} time 延长的时间
	 * @param {*} type 延长单位，如d:天，m:分钟
	 * @param {*} format 时间格式
	 */
	getExtendTime: (date, time, type, format) => {
		return moment(date).add(time, type).format(format);
	},
	/**
	 * 时间倒计时
	 * @param {*} endtime 时间
	 * @param {*} format 时间
	 * @param {*} outTip 时间
	 */
	NewOrderInitTime: (endtime, format = "mm-ss", outTip = "00分00秒") => {
		// 倒计时初始化
		var hh = null,
			mm = null,
			ss = null
		let str = ''
		var time = parseInt(filter.getTimestamp(endtime)) - new Date().getTime();
		if (time <= 0) {
			return outTip;
		} else {
			hh = Math.floor(time / 1000 / 60 / 60 / 24) * 24 +
				Math.floor((time / 1000 / 60 / 60) % 24);
			ss = Math.floor((time / 1000) % 60);
			if (format == "mm-ss") {
				mm = hh * 60 + Math.floor((time / 1000 / 60) % 60);
				str = mm + "分" + ss + "秒";
				return str;
			} else if (format == "hh-mm") {
				mm = Math.floor((time / 1000 / 60) % 60);
				str = hh + "时" + mm + "分";
			}
			return str;
		}
	},
	/**
	 * 字符替换
	 * @param {*} chars 字符串
	 * @param {*} reword 被替换内容
	 * @param {*} tochar 替换内容
	 */
	charReword: (chars, reword, tochar) => {
		let label = chars.replace(new RegExp(reword, 'g'), tochar)
		return label
	},
	/**
	 * 截取字符串变数组
	 * @param {*} chars 字符串
	 * @param {*} tochar 截取内容
	 */
	charToArray: (chars, tochar) => {
		let array = chars.split(tochar)
		return array
	},
	/**
	 * 下标转中文
	 * @param {*} index 下标
	 */
	setTitle: (index) => {
		let label = ""
		switch (Number(index)) {
			case 0:
				label = '新订单';
				break;
			case 1:
				label = '已报价';
				break;
			case 2:
				label = '待出票';
				break;
			case 3:
				label = '已出票';
				break;
			case 4:
				label = '已关闭';
				break;
		}
		return label
	},
	/**
	 * 时间对比
	 * @param {*} startTime 时间
	 * @param {*} endTime 对比时间 (默认当前时间)
	 */
	timeCompare: (startTime, endTime = null) => {
		let result = ''
		startTime = new Date(filter.formateDate(startTime, 'YYYY/MM/DD HH-mm-ss'));
		if (endTime == null) {
			endTime = new Date()
		} else {
			endTime = new Date(filter.formateDate(endTime, 'YYYY/MM/DD HH-mm-ss'));
		}
		if (startTime.getTime() < endTime.getTime()) {
			result = -1
		} else if (startTime.getTime() == endTime.getTime()) {
			result = 0
		} else {
			result = 1
		}
		return result
	},
	/**
	 * 手机号*加密
	 * @param {*} mobile 手机号
	 */
	mobileHide: (mobile) => {
		var reg = /^(\d{3})\d{4}(\d{4})$/;
		let result = ''
		if (mobile) {
			result = mobile.replace(reg, "$1****$2");
		}
		return result
	},
	/**
	 * 计算成交率
	 * @param {*} ctotal 成交量
	 * @param {*} stotal 发起数
	 */
	calcCtotalRate: (ctotal, stotal) => {
		let result = ((Number(ctotal) / Number(stotal)) * 100).toFixed(2)
		return result
	},
	/**
	 * 保留两位小数
	 * @param {*} num 数
	 */
	keepTwoDecimal: (num) => {
		return Number(num).toFixed(2)
	},
	/**
	 * 流水状态
	 * @param {*} type 类型
	 * @param {*} status 提现状态
	 */
	payType: (type, status = 0) => {
		let label = ''
		switch (Number(type)) {
			case 0:
				if (status == 0) {
					label = '提现成功'
				} else if (status == 1) {
					label = '提现失败'
				}
				break;
			case 1:
				label = '出票收入';
				break;
			case 2:
				label = '系统扣除';
				break;
		}
		return label
	},
	/**
	 * 上万数字变万
	 * @param {*} num 数值
	 */
	trannumber: (num) => {
		if(num) {
			let numStr = num.toString()
			let rusult = ''
			// 千以内直接返回
			if (numStr.length <= 4) {
				return numStr;
			}
			//大于8位数是亿
			else if (numStr.length > 8) {
				let decimal = numStr.substring(numStr.length - 8);
				rusult = parseFloat(Number(num / 100000000).toFixed(2) + '.' + decimal) + '亿';
			}
			//大于6位数是十万 (以10W分割 10W以下全部显示)
			else if (numStr.length > 4) {
				let decimal = numStr.substring(numStr.length - 4)
				rusult = parseFloat(Number(num / 10000).toFixed(1) + '.' + decimal) + '万';
			}
			return rusult
		}
	},
	/**
	 * 计算两点距离
	 * @param {*} lat 经度
	 * @param {*} lng 纬度
	 */
	calcDistance: (lat, lng) => {
		let longitude = ''
		let latitude = ''
		let distance = ''
		uni.getLocation({
			type: 'wgs84',
			success: function(res) {
				latitude = res.latitude
				longitude = res.longitude
			}
		});
		distance = common.getMapDistance(latitude, longitude, lat, lng)
	},
	/**
	 * 内容超出长度用...显示
	 * @param {*} value 字符
	 * @param {*} length 长度
	 */
	ellipsis: (value, length = 68) => {
		if (!value) return "";
		if (value.length > length) {
			return value.slice(0, length) + "......";
		}
		return value;
	},
	/**
	 * px和rpx相互转换
	 * @param {*} value 字符
	 * @param {*} type 要转的类型
	 */
	pxChangeRpx: (value, type) => {
		let result = ''
		switch(type){
			case 'px': 
				result = uni.upx2px(value);
				break
			case 'rpx': 
				result = value/(uni.upx2px(value)/value) - 1;
				break
		}
		return result
	},
	/**
	 * 字符替换
	 * @param {*} value 字符
	 * @param {*} char1 被替换字符
	 * @param {*} char2 替换字符
	 * @param {*} all 是否全部替换
	 */
	shufilter:(value, char1 = '|',char2 = '/', all = false)=>{
		let chars = value
		if(value) {
			if(!all) {
				chars = value.replace(char1, char2)
			} else {
				chars = value.replace(new RegExp("\\" + char1, 'gi'), char2)
			}
		}
		return chars
	},
	
	/**
	 * 设置座位图片
	 * @param {*} seat 作座类型
	 * @param {*} state 座位状态
	 */
	setSeatImg: (seat, state) => {
		//-1为非座位，0为未购座位，1为已选座位(绿色),2为已购座位(红色)
		let img = ''
		let data = [
			{
				'0': require('@/static/img/other/normal_seat.png'),
				'1': require('@/static/img/other/normal_seat_choose.png'),
				'2': require('@/static/img/other/normal_seat_active.png'),
			},
			{
				'0': require('@/static/img/other/couple2_seat_left.png'),
				'1': require('@/static/img/other/couple2_left_choose.png'),
				'2': require('@/static/img/other/couple2_left_active.png'),
			},
			{
				'0': require('@/static/img/other/couple2_seat_right.png'),
				'1': require('@/static/img/other/couple2_right_choose.png'),
				'2': require('@/static/img/other/couple2_right_active.png'),
			},
		]
		img = data[seat][state]
		return img
	},
	
	
	// 放映结束时间
	endTime:(start,dur)=>{
		let startStamp = moment(start).format('x')
		let res = startStamp * 1 + dur * 60 * 1000
		res = moment(res).format()
		res = moment(res).utcOffset(8).format('HH:mm')
		return res
	}
}
export default filter
